package org.example;

import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;

public class Test {
    public static void main(String[] args) throws IOException {
        ArrayList<Student> list = new ArrayList<>();
        BufferedReader br=new BufferedReader(new FileReader("文件路径"));
        String line;
        while((line=br.readLine())!=null){
            String[] str=line.split("-");
            Student s=new Student(str[0],str[1],Integer.parseInt(str[2]),Double.parseDouble(str[3]));
            list.add(s);
        }
        br.close();
        //计算权重总和
        double weight = 0;
        for (Student student : list) {
            weight += student.getWeight();
        }
        //计算每个学生权重
        double []arr=new double[list.size()];
        int index=0;
        for (Student student : list) {
            arr[index]=student.getWeight()/weight;
            index++;
        }
        //计算每个人权重占比范围
        for (int i = 0; i < arr.length; i++) {
            if(i==0){
                arr[i]=arr[i];
            }else{
                arr[i]=arr[i-1]+arr[i];
            }
        }
        //随机抽取 获取0.0~1.0之间随机数
        double number=Math.random();
        //判断随机数在哪个范围 二分查找法
        //方法返回值：-插入点-1
        int result=-1- Arrays.binarySearch(arr,number);
        Student stu=list.get(result);
        //修改当前学生权重
        stu.setWeight(stu.getWeight()/2);
        //将集合数据再次写入文件
        BufferedWriter bw=new BufferedWriter(new FileWriter("文件路径"));
        for (Student student : list) {
            bw.write(student.toString());
            bw.newLine();
        }
        bw.close();
    }
}